package com.wx.tnsbasedata.service.impl;

import com.wx.tnsbasedata.mapper.JobMapper;
import com.wx.tnsbasedata.model.CityModel;
import com.wx.tnsbasedata.model.JobModel;
import com.wx.tnsbasedata.model.vo.CityVo;
import com.wx.tnsbasedata.model.vo.EntJobVo;
import com.wx.tnsbasedata.service.JobService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @author 林建权
 * @version 1.0
 * @date 2020/7/25 20:19
 */
@Service
public class JobServiceImpl implements JobService {

    @Resource
    private JobMapper jobMapper;

    //查询全部城市，以特定格式返回
    @Override
    public JobModel findAll() {
        //查询数据
        List<JobModel> jobList = jobMapper.findAll();
        //数据封装为HashMap<Integer,JobModel>对象
        HashMap<Integer, JobModel> map = new HashMap<>();
        for(JobModel jm:jobList){
            map.put(jm.getId(),jm);
        }
        for (Integer id:map.keySet()){
            JobModel job = map.get(id);
            if (job.getPid()!=0){
                //不是顶级父id
                //找到父级职业
                JobModel jobModel = map.get(job.getPid());
                //加为其子级
                if (jobModel.getChildren()==null){
                    //将顶级父id存入列表
                    jobModel.setChildren(new ArrayList<>());
                }
                jobModel.getChildren().add(job);
            }
        }
        return map.get(1);
    }

    @Override
    public int insert(JobModel jobModel) {
        return jobMapper.insert(jobModel);
    }

    @Override
    public int update(JobModel jobModel) {
        return jobMapper.update(jobModel);
    }

    @Override
    public int delete(Integer id) {
        return jobMapper.delete(id);
    }

    @Override
    public JobModel findById(Integer id) {
        return jobMapper.findById(id);
    }

    @Override
    public List<EntJobVo> allTypeOne() {
        return jobMapper.allTypeOne();
    }
}
